package test5;
//StringBuilder用法
public class Test {

    // StringBuilder或StringBuffer转为String使用toString方法
    public static String func2(){
        StringBuilder sb=new StringBuilder();
        return sb.toString();
    }

    //String转换为StringBuffer或StringBuilder，使用构造方法
    public static StringBuffer func(){
        String str="abcdef";
        return new StringBuffer(str);
    }

    public static void main(String[] args) {
        String str="abcd";

    }


    public static void main3(String[] args) {
        StringBuilder sb=new StringBuilder("abcdef");
        sb.reverse();//String没有reverse方法
        System.out.println(sb);
        StringBuffer sb2=new StringBuffer();

    }


    public static void main2(String[] args) {
        String str="abcd";
        str += "123";
        System.out.println(str);//abcd123 【普通的String拼接底层会被优化为StringBuilder】
    }

    public static void main1(String[] args) {
        StringBuilder sb=new StringBuilder();
        sb.append("abcdef");
        sb.append("1234");//返回的还是原来的对象，没有new
        System.out.println(sb.toString());
    }
}
